/*
 *  Copyright 2013 National Institute of Advanced Industrial Science and Technology
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.sss.dummykvs;

import java.io.IOException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sss.mapreduce.DB;
import org.sss.util.Misc;

public class DummyDBServer {

  private static Logger logger;

  private static class RemoteDBImpl extends UnicastRemoteObject implements RemoteDB{
    protected RemoteDBImpl() throws RemoteException {
      super();
    }

    private static final long serialVersionUID = 1L;
    private DB db = new TreeMapDBBackend();

    @Override
    public void putlist(byte[][] kvs) throws IOException {
      db.putlist(kvs);
    }

    @Override
    public List<byte[]> range(byte[] prefix, byte[] sentinel, int limit,
        boolean binc) throws IOException {
      return db.range(prefix, sentinel, limit, binc);
    }

    @Override
    public List<byte[]> ranged(byte[] prefix, byte[] sentinel, int limit,
        boolean binc) throws IOException {
      return db.ranged(prefix, sentinel, limit, binc);
    }

    @Override
    public int rangeout(byte[] prefix, byte[] sentinel, int count,
        boolean binc) throws IOException {
      return db.rangeout(prefix, sentinel, count, binc);
    }

    @Override
    public List<byte[]> fwmkeys(byte[] prefix, int max) throws IOException {
      return db.fwmkeys(prefix, max);
    }

    @Override
    public byte[] get(byte[] key) throws IOException {
      return db.get(key);
    }

    @Override
    public void close() throws IOException {
      // Do nothing
    }

    @Override
    public List<byte[]> getlist(byte[][] keys) throws IOException {
      return db.getlist(keys);
    }

    @Override
    public void outlist(byte[][] keys) throws IOException {
      db.outlist(keys);
    }
  }

  public static void main(String[] args) throws Exception{

    //      PropertyConfigurator.configure(
    //          Thread.currentThread().getContextClassLoader()
    //            .getResource("conf/mapreduce.log4j.dummydb.properties"));
    logger = LoggerFactory.getLogger(DummyDBServer.class);

    logger.info("Invoked");

    if (args.length < 1) {
      System.err.println("Usage: java Server PORT");
      System.exit(1);
    }

    // ***Caution! this is extremely dangerous!***
    Misc.setDangerousSecurityManagerToSystem();

    Registry r = LocateRegistry.createRegistry(Integer.parseInt(args[0]));
    r.bind("db", new RemoteDBImpl());
  }
}
